/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db.tasks;

import java.awt.datatransfer.StringSelection;
import pl.cezary.butler.walili.common.User;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.cezary.butler.walili.server.db.Queries;
import pl.cezary.butler.walili.server.db.ZadanieSerwerBD;

/**
 * HSQL DB COMPAT
 *
 * @author Przemysław Bałdyga
 */
public class DeleteUserTask extends UpdateTask {

    private static final Logger log = Logger.getLogger(DeleteUserTask.class.getName());
    private final String LOGIN;
    private final String HASLO;
    private final int USER_ID;

    private DeleteUserTask(User user) {
        LOGIN = user.getLogin();
        HASLO = user.getHaslo();
        USER_ID = 0;
    }

    private DeleteUserTask(final int user_id) {
        assert (user_id > 0);
        LOGIN = null;
        HASLO = null;
        USER_ID = user_id;
    }
    
    public static UpdateTask createTaskDeleteUserById(final int userId){
        return new DeleteUserTask(userId);
    }
    
    public static UpdateTask createTaskDeleteUserById(final IUserId userId){
        return new DeleteUserTask((userId.getId() == -1) ? 0 : userId.getId());
    }
    
    public static UpdateTask createTaskDeleteUserByCredentials(final User user){
        return new DeleteUserTask(user);
    }

    @Override
    public void execute() {
        int rows;
        if (USER_ID > 0) {
            rows = getJdbcTemplate().update(Queries.DELETE_USER_ID.getQuery(), USER_ID);
        } else {
            assert(LOGIN!=null && HASLO != null);
            rows = getJdbcTemplate().update(Queries.DELETE_USER_CREDENTIALS.getQuery(), LOGIN, HASLO);
        }
        if(rows==0){
            log.warning("No user deleted for data id: {0}, login: {1}, password: {2};");
        }
    }
}
